package com.baiku.sanhos.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@Service
public class ProfilePictureStorageService {

    @Value("${profile.picture.storage.path}")
    private String storagePath; // 用于配置头像存储路径

    public String storeProfilePicture(byte[] imageBytes, String username) {
        try {
            // 生成新的文件名
            String fileName = username + "-" + System.currentTimeMillis() + ".jpg";
            // 设置文件存储的完整路径
            String filePath = storagePath + fileName;

            // 将图像数据保存到指定路径
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(imageBytes);
            fos.close();

            return filePath;
        } catch (IOException e) {
            e.printStackTrace();
            // 存储失败时返回null或错误信息
            return null;
        }
    }

    public File getProfilePicture(String fileName) {
        return new File(storagePath + File.separator + fileName);
    }
}


